/*
 * 在沙盘中很多CPU耗时卡在ClassManager.Get()
 * 对于频繁获取的类池，应保持高性能。省略掉管理、泛型代码。区别于ClassManager
 */

using System.Collections.Generic;

namespace WorldMapBase.Tile.Pool
{
    public class ClassPoolManager
    {
        private static TileShowCmdPool _tileShowCmdPool;
        private static TileHideCmdPool _tileHideCmdPool;
        private static TileCommandNodePool _tileCommandNodePool;
        
        private static CommandTileElementPool _commandTileElementPool;

        private static WorldObjectPool _worldObjectPool;
        private static TreeObjectPool _treeObjectPool;
        //private static TrafficObjectPool _trafficObjectPool;

        private static AdornObjectPool _adornObjectPool;
        public static void Init()
        {

            _tileShowCmdPool = new TileShowCmdPool(100);
            _tileShowCmdPool.OnInit(100);
            _tileHideCmdPool = new TileHideCmdPool(100);
            _tileHideCmdPool.OnInit(100);
            _tileCommandNodePool = new TileCommandNodePool(200);
            _tileCommandNodePool.OnInit(200);
            
            _commandTileElementPool = new CommandTileElementPool(300);
            _commandTileElementPool.OnInit(300);
            
            _worldObjectPool = new WorldObjectPool(300);
            _worldObjectPool.OnInit(300);
            
            _treeObjectPool = new TreeObjectPool(200);
            _treeObjectPool.OnInit(200);



            _adornObjectPool = new AdornObjectPool(200);
            _adornObjectPool.OnInit(200);
        }

        public static void Destory()
        {
            _tileShowCmdPool.Destroy();
            _tileShowCmdPool = null;
            
            _tileHideCmdPool.Destroy();
            _tileHideCmdPool = null;
            
            _tileCommandNodePool.Destroy();
            _tileCommandNodePool = null;
            
            _commandTileElementPool.Destroy();
            _commandTileElementPool = null;
            
            _worldObjectPool.Destroy();
            _worldObjectPool = null;
            
            _treeObjectPool.Destroy();
            _treeObjectPool = null;

            //_trafficObjectPool.Destroy();
            //_trafficObjectPool = null;

            _adornObjectPool.Destroy();
            _adornObjectPool = null;
        }
        #region TileShowCmd

        public class TileShowCmdPool : ClassObjectPool<TileShowCmd>
        {
            public TileShowCmdPool(int capacity) : base(capacity)
            {
            }

            protected override void OnReset(TileShowCmd obj)
            {
                obj.OnReset();
            }
        }

        
        public static TileShowCmd GetTileShowCmd()
        {
            return _tileShowCmdPool.GetItem();
        }

        public static void RecoverTileShowCmd(TileShowCmd obj)
        {
            _tileShowCmdPool.RecoverItem(obj);
        }
        
        #endregion


        #region TileHideCmd
        
        public class TileHideCmdPool : ClassObjectPool<TileHideCmd>
        {
            public TileHideCmdPool(int capacity) : base(capacity)
            {
            }

            protected override void OnReset(TileHideCmd obj)
            {
                obj.OnReset();
            }
        }
        
        public static TileHideCmd GetTileHideCmd()
        {
            return _tileHideCmdPool.GetItem();
        }

        public static void RecoverTileHideCmd(TileHideCmd obj)
        {
            _tileHideCmdPool.RecoverItem(obj);
        }
        
        #endregion


        #region CommandTileElement

        public class CommandTileElementPool : ClassObjectPool<CommandTileElement>
        {
            public CommandTileElementPool(int capacity) : base(capacity)
            {
            }

            protected override void OnReset(CommandTileElement obj)
            {
                obj.OnReset();
            }
        }
        
        public static CommandTileElement GetCommandTileEle()
        {
            return _commandTileElementPool.GetItem();
        }

        public static void RecoverCommandTileEle(CommandTileElement obj)
        {
            _commandTileElementPool.RecoverItem(obj);
        }

        #endregion


        #region WorldObject

        public class WorldObjectPool : ClassObjectPool<WorldObject>
        {
            public WorldObjectPool(int capacity) : base(capacity)
            {
            }

            protected override void OnReset(WorldObject obj)
            {
                obj.OnReset();
            }
        }

        public static WorldObject GetWorldObject()
        {
            return _worldObjectPool.GetItem();
        }

        public static void RecoverWorldObject(WorldObject obj)
        {
            _worldObjectPool.RecoverItem(obj);
        }

        #endregion


        #region TreeObject

        public class TreeObjectPool : ClassObjectPool<TreeObject>
        {
            public TreeObjectPool(int capacity) : base(capacity)
            {
            }

            protected override void OnReset(TreeObject obj)
            {
                obj.OnReset();
            }
        }

        public static TreeObject GetTreeObject()
        {
            return _treeObjectPool.GetItem();
        }

        public static void RecoverTreeObject(TreeObject obj)
        {
            _treeObjectPool.RecoverItem(obj);
        }

        #endregion


        #region TileCommandNode
        //LinkedList<TileCommandBase>

        public class TileCommandNodePool : ObjectPool<LinkedListNode<TileCommandBase>>
        {
            public TileCommandNodePool(int capacity) : base(capacity)
            {
#if UNITY_EDITOR
                _name = "TileCommandNodePool: LinkedListNode<TileCommandBase>";
#endif
            }

            protected override LinkedListNode<TileCommandBase> ConstructItem()
            {
                return new LinkedListNode<TileCommandBase>(null);
            }

            protected override void BeforeRecover(LinkedListNode<TileCommandBase> obj)
            {
                obj.Value = null;
            }
        }

        public static LinkedListNode<TileCommandBase> GetTileCommandNode()
        {
            return _tileCommandNodePool.GetItem();
        }

        public static void RecoverTileCommandNode(LinkedListNode<TileCommandBase> obj)
        {
            _tileCommandNodePool.RecoverItem(obj);
        }

        #endregion

        #region TrafficObject

        //public class TrafficObjectPool : ClassObjectPool<TrafficObject>
        //{
        //    public TrafficObjectPool(int capacity) : base(capacity)
        //    {
        //    }

        //    protected override void OnReset(TrafficObject obj)
        //    {
        //        obj.OnReset();
        //    }
        //}

        //public static TrafficObject GetTrafficObject()
        //{
        //    return _trafficObjectPool.GetItem();
        //}

        //public static void RecoverTrafficObject(TrafficObject obj)
        //{
        //    _trafficObjectPool.RecoverItem(obj);
        //}

        #endregion

        #region AdornObject

        public class AdornObjectPool : ClassObjectPool<AdornObject>
        {
            public AdornObjectPool(int capacity) : base(capacity)
            {
            }

            protected override void OnReset(AdornObject obj)
            {
                obj.OnReset();
            }
        }

        public static AdornObject GetAdornObject()
        {
            return _adornObjectPool.GetItem();
        }

        public static void RecoverGetAdornObject(AdornObject obj)
        {
            _adornObjectPool.RecoverItem(obj);
        }

        #endregion
    }
}